<html>
<head><meta charset="utf-8"><title>Check if type implements trait · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20type.20implements.20trait.html">Check if type implements trait</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="224359917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20type%20implements%20trait/near/224359917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kevin Tran <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20type.20implements.20trait.html#224359917">(Jan 28 2021 at 17:14)</a>:</h4>
<p>Suppose I have a value of type <code>Ty</code> in a method for <code>MirBorrowckCtxt</code>. How can I check if the type implements a trait given by name, like <code>Deref</code>?</p>



<a name="224411789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20type%20implements%20trait/near/224411789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20type.20implements.20trait.html#224411789">(Jan 28 2021 at 23:57)</a>:</h4>
<p><code>Deref</code> is a language item, so if you're looking for it you can get its <code>DefId</code> with <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/lang_items/struct.LanguageItems.html#method.deref_trait">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/lang_items/struct.LanguageItems.html#method.deref_trait</a>.</p>



<a name="224436194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20type%20implements%20trait/near/224436194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kevin Tran <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20type.20implements.20trait.html#224436194">(Jan 29 2021 at 06:53)</a>:</h4>
<p>Once I have the DefId, how do I check if a type implements a trait?</p>



<a name="224468945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20type%20implements%20trait/near/224468945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20type.20implements.20trait.html#224468945">(Jan 29 2021 at 13:21)</a>:</h4>
<p><span class="user-mention" data-user-id="351526">@Kevin Tran</span> This is how it's done for the builtin <code>Copy</code> / <code>Freeze</code> traits: <a href="https://github.com/rust-lang/rust/blob/c4e33b51c1a2d5e599b949fa3006467b88df253a/compiler/rustc_middle/src/ty/util.rs#L679">https://github.com/rust-lang/rust/blob/c4e33b51c1a2d5e599b949fa3006467b88df253a/compiler/rustc_middle/src/ty/util.rs#L679</a>. Maybe this helps?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>